﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookExchange.Business;
using BookExchange.CustomClass.Base;
using BookExchange.ODataService;
using BookExchange.Utility;
using System.Linq.Expressions;
using BookExchange.ViewModel;
using BookExchange.Configuration;

namespace BookExchange
{
    public partial class SearchAll : BasePage
    {
        #region formatString
        private string _formatDate = "{0:" + AppConfiguration.Instance.DateFormat + "}";
        private string _formatQuantity = "{0:" + AppConfiguration.Instance.IntegerFormat + "}";
        private string _formatPrice1 = "{0:" + AppConfiguration.Instance.CurrencyFormat + "}";
        private string _formatPrice2 = "{0:" + AppConfiguration.Instance.CurrencyFormat + "}";

        public string formatDate
        {
            get { return _formatDate; }

        }
        public string formatQuantity
        {
            get { return _formatQuantity; }
        }
        public string formatPrice1
        {
            get { return _formatPrice1; }
        }
        public string formatPrice2
        {
            get { return _formatPrice2; }
        }

        #endregion


        private RequestBusiness requestBus;
        static IQueryable<ODataService.Request> query;
        static private int itemPerPage = AppConfiguration.Instance.MaxItemPerPageSearchAll;
 
        public SearchAll()
            : base()
        {
            requestBus = new RequestBusiness();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            string searchText = string.Empty;
      
            //caculate skip value and current page
            int skipFromSession = GetSession();
            int currentPage = (int)(skipFromSession / itemPerPage);
            int skip = currentPage * itemPerPage;

            query = requestBus.GetQueryDefault();

            //check and get query of MemberCode field
            if (!string.IsNullOrEmpty(searchText = Request.QueryString["myrequest"]) && searchText == "1")//search all by user
            {

                if (Session[AppConstant.CurrentUser] != null)
                {
                    query = requestBus.GetQueryFilterUser(query, CurrentMember.MemberCode );
                }
                else
                {
                    //Member chưa đăng nhập;
                    return;
                }
            }

            //check and get query of CategoryCode field
            int category = 0;
            if (int.TryParse(Request.QueryString["cat"], out category))//search by category
            {
                query = requestBus.GetQueryFilterCategory(query, category );

            }
            //check and get query of Name field
            if (!string.IsNullOrEmpty(searchText = Request.QueryString["name"]))//search by name
            {
                query = requestBus.GetQueryFilterName(query, searchText );
            }

            //check and get query of RequestType field
            if (!string.IsNullOrEmpty(searchText = Request.QueryString["allpost"]) && searchText == "1")
            {
                query = requestBus.GetQueryFilterRequestType(query, RequestType.Post );

                BindData(skip);

                int countPost = GetSessionCount();
                if(countPost == -1)
                 countPost = requestBus.GetCountActiveRequest(query);

                lblPostQty.Text = "Sách cần bán ( " + countPost.ToString() + " )" + string.Empty;

                AspNetPagerPost.RecordCount = countPost;
                AspNetPagerPost.PageSize = itemPerPage;


                AspNetPagerPost.CurrentPageIndex = currentPage + 1;
            }
            else if (!string.IsNullOrEmpty(searchText = Request.QueryString["allrequest"]) && searchText == "1")
            {
                query = requestBus.GetQueryFilterRequestType(query, RequestType.Request );

                BindData(skip);

                int countPost = GetSessionCount();
                if (countPost == -1)
                    countPost = requestBus.GetCountActiveRequest(query); 
                lblPostQty.Text = "Sách cần mua ( " + countPost.ToString() + " )" + string.Empty;

                AspNetPagerPost.RecordCount = countPost;
                AspNetPagerPost.PageSize = itemPerPage;

                AspNetPagerPost.CurrentPageIndex = currentPage + 1;
            }
            else
            {
                Response.Redirect("Search.aspx" + Request.Url.Query);
            }



        }
    
        private void BindData(int skipItem)
        {
            List<Request> postList = requestBus.GetListActiveRequest(query, skipItem, itemPerPage);
            repPost.DataSource = postList;
            repPost.DataBind();
        }

        protected void AspNetPagerPost_PageChanged(object src, EventArgs e)
        {
            int skip = AspNetPagerPost.StartRecordIndex - 1;
            BindData(skip);

        }

        private int GetSessionCount()
        {
            if (Session["RequestCount"] != null)
            {
                int temp;
                if (int.TryParse(Session["RequestCount"].ToString(), out temp))
                {
                    Session.Remove("RequestCount");
                    return temp;
                }
                else
                {
                    Session.Remove("RequestCount");
                    return -1;
                }
            }
            else
                return -1;
        }


        private int GetSession()
        {
            if (Session["skip"] != null)
            {
                int temp;
                if (int.TryParse(Session["skip"].ToString(), out temp))
                {
                    Session.Remove("skip");
                    return temp;
                }
                else
                {
                    Session.Remove("skip");
                    return 0;
                }
            }
            else
                return 0;
        }
    }
}